﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;

public partial class Register : WebPageBase
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownListRole.DataBind();
            VisibleControlsByRole();
        }
    }

    protected void ButtonRegister_Click(object sender, EventArgs e)
    {
        UserQ user = db.UserQs.Where(u => u.Email.Equals(TextBoxEmail.Text)).FirstOrDefault();

        if (user == null)
        {
            try
            {

                UserQ newUser = new UserQ();

                int userId = (from u in db.UserQs select u.Id).Max() + 1;
                newUser.Id = userId;
                newUser.Email = TextBoxEmail.Text;
                newUser.Password = TextBoxPassword.Text;
                newUser.Name = TextBoxName.Text;
                newUser.Surname1 = TextBoxSurname1.Text;
                newUser.Surname2 = TextBoxSurname2.Text;
                newUser.RoleId = Convert.ToInt32(DropDownListRole.SelectedValue);
                newUser.RegisterDate = DateTime.Now;

                if (DropDownListRole.SelectedValue.Equals("2"))//Se escogio un lider
                {
                    newUser.UniversityId = int.Parse(DropDownListUniversity.SelectedValue);
                    if (base.UserSystem.RoleId == 3) //Coordinator
                    {
                        newUser.ParentId = base.UserSystem.Id;
                    }
                    else
                    {
                        if (base.UserSystem.RoleId == 4) //Supervisor
                        {
                            newUser.ParentId = int.Parse(DropDownListCoordinator.SelectedValue);
                        }
                    }
                }

                base.db.UserQs.InsertOnSubmit(newUser);

                db.SubmitChanges();

                TextBoxEmail.Text = String.Empty;
                TextBoxPassword.Text = String.Empty;
                TextBoxName.Text = String.Empty;
                TextBoxSurname1.Text = String.Empty;
                TextBoxSurname2.Text = String.Empty;
                DropDownListRole.SelectedIndex = 0;

                Message1.Message = "Sus datos han sido registrados.";
                Message1.Type = Constant.MESSAGE_OK;
                
            }
            catch (Exception ex)
            {

            }
        }
        else
        {
            Message1.Message = "El correo ya existe en nuestro sistema.";
            Message1.Type = Constant.MESSAGE_ERROR;
        }
    }

    protected void DropDownListRole_SelectedIndexChanged(object sender, EventArgs e)
    {
        VisibleControlsByRole();
    }

    protected void DropDownListCoordinator_DataBound(object sender, EventArgs e)
    {
        if (DropDownListCoordinator.Items.Count > 0)
        {
            for (int i = 0; i < DropDownListCoordinator.Items.Count; i++)
            {
                UserQ user = db.UserQs.First(u => u.Id == int.Parse(DropDownListCoordinator.Items[i].Value));
                DropDownListCoordinator.Items[i].Text = user.Name + " " + user.Surname1 + " " + user.Surname2;
            }

        }
    }

    public void VisibleControlsByRole()
    {
        if ((base.UserSystem.RoleId == 4) && DropDownListRole.SelectedValue.Equals("2"))
        {
            trRowLider.Visible = true;
        }
        else
        {
            trRowLider.Visible = false;
        }

        if (base.UserSystem.RoleId == 3)
        {
            DropDownListCoordinator.Visible = false;
            LiteralCoordinator.Visible = false;
        }

    }
    
}
